<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>协程 | ShuangChenYue</title>
    <meta name="generator" content="VuePress 1.9.10">
    <link rel="icon" href="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg">
    <meta name="description" content="满招损，谦受益">
    <meta name="keywords" content="专注于Cpp语言的旅行者">
    
    <link rel="preload" href="/assets/css/0.styles.952d6952.css" as="style"><link rel="preload" href="/assets/js/app.67adcfd9.js" as="script"><link rel="preload" href="/assets/js/4.9aaa1650.js" as="script"><link rel="preload" href="/assets/js/1.5474518c.js" as="script"><link rel="preload" href="/assets/js/3.593d14fc.js" as="script"><link rel="preload" href="/assets/js/52.54cc6e51.js" as="script"><link rel="prefetch" href="/assets/js/10.3242746b.js"><link rel="prefetch" href="/assets/js/100.9224de43.js"><link rel="prefetch" href="/assets/js/101.f0d1b059.js"><link rel="prefetch" href="/assets/js/102.996bfc6d.js"><link rel="prefetch" href="/assets/js/103.9bfdbd6f.js"><link rel="prefetch" href="/assets/js/104.8613f283.js"><link rel="prefetch" href="/assets/js/105.aa6e809e.js"><link rel="prefetch" href="/assets/js/106.90192392.js"><link rel="prefetch" href="/assets/js/107.e82a40b7.js"><link rel="prefetch" href="/assets/js/108.994cd438.js"><link rel="prefetch" href="/assets/js/109.ec15acc2.js"><link rel="prefetch" href="/assets/js/11.c04b41c1.js"><link rel="prefetch" href="/assets/js/110.c32d8576.js"><link rel="prefetch" href="/assets/js/111.453b5d50.js"><link rel="prefetch" href="/assets/js/112.ffbdb3a4.js"><link rel="prefetch" href="/assets/js/113.12b8ad7d.js"><link rel="prefetch" href="/assets/js/114.899d2998.js"><link rel="prefetch" href="/assets/js/115.b7ad9576.js"><link rel="prefetch" href="/assets/js/116.a8394748.js"><link rel="prefetch" href="/assets/js/117.0edfe25b.js"><link rel="prefetch" href="/assets/js/118.9161b1fe.js"><link rel="prefetch" href="/assets/js/119.be59e21b.js"><link rel="prefetch" href="/assets/js/12.41437bf6.js"><link rel="prefetch" href="/assets/js/120.bcf439fb.js"><link rel="prefetch" href="/assets/js/121.c3d251b8.js"><link rel="prefetch" href="/assets/js/122.62b1caba.js"><link rel="prefetch" href="/assets/js/123.787c2ab0.js"><link rel="prefetch" href="/assets/js/124.a880746f.js"><link rel="prefetch" href="/assets/js/125.d8edfe7b.js"><link rel="prefetch" href="/assets/js/126.4ff01546.js"><link rel="prefetch" href="/assets/js/127.9416d1ff.js"><link rel="prefetch" href="/assets/js/128.01a4a7a0.js"><link rel="prefetch" href="/assets/js/129.76876665.js"><link rel="prefetch" href="/assets/js/13.922328e9.js"><link rel="prefetch" href="/assets/js/130.7f631dd9.js"><link rel="prefetch" href="/assets/js/131.c9e0fde9.js"><link rel="prefetch" href="/assets/js/132.1c04cde5.js"><link rel="prefetch" href="/assets/js/133.e8f381cd.js"><link rel="prefetch" href="/assets/js/134.03d19f8b.js"><link rel="prefetch" href="/assets/js/135.44607494.js"><link rel="prefetch" href="/assets/js/136.6a1eb3c9.js"><link rel="prefetch" href="/assets/js/137.27898fd1.js"><link rel="prefetch" href="/assets/js/138.5bc0cf54.js"><link rel="prefetch" href="/assets/js/139.c2d1addc.js"><link rel="prefetch" href="/assets/js/14.e54d7526.js"><link rel="prefetch" href="/assets/js/140.052ec8e4.js"><link rel="prefetch" href="/assets/js/141.131abb5a.js"><link rel="prefetch" href="/assets/js/142.6ba6c07b.js"><link rel="prefetch" href="/assets/js/143.5dd51d22.js"><link rel="prefetch" href="/assets/js/144.b45afca8.js"><link rel="prefetch" href="/assets/js/145.faa9fb04.js"><link rel="prefetch" href="/assets/js/146.b54c024d.js"><link rel="prefetch" href="/assets/js/147.a1223242.js"><link rel="prefetch" href="/assets/js/148.4767bcb2.js"><link rel="prefetch" href="/assets/js/149.b65ab046.js"><link rel="prefetch" href="/assets/js/15.7082a3da.js"><link rel="prefetch" href="/assets/js/150.9bd8c175.js"><link rel="prefetch" href="/assets/js/151.9f830e96.js"><link rel="prefetch" href="/assets/js/152.41cde7f0.js"><link rel="prefetch" href="/assets/js/153.f57d65e0.js"><link rel="prefetch" href="/assets/js/154.5d7c8d51.js"><link rel="prefetch" href="/assets/js/155.0ae99532.js"><link rel="prefetch" href="/assets/js/156.5a54e043.js"><link rel="prefetch" href="/assets/js/157.c25b5d40.js"><link rel="prefetch" href="/assets/js/158.aa025b46.js"><link rel="prefetch" href="/assets/js/159.47939d88.js"><link rel="prefetch" href="/assets/js/16.fc775b7b.js"><link rel="prefetch" href="/assets/js/160.f8624459.js"><link rel="prefetch" href="/assets/js/161.7a075dc2.js"><link rel="prefetch" href="/assets/js/162.1d48f266.js"><link rel="prefetch" href="/assets/js/163.5d68a99f.js"><link rel="prefetch" href="/assets/js/164.1262d0e5.js"><link rel="prefetch" href="/assets/js/165.2ccf0bdd.js"><link rel="prefetch" href="/assets/js/166.21ece4d9.js"><link rel="prefetch" href="/assets/js/167.bf8adb95.js"><link rel="prefetch" href="/assets/js/168.1cb8440d.js"><link rel="prefetch" href="/assets/js/169.1dd1e396.js"><link rel="prefetch" href="/assets/js/17.ecc7be70.js"><link rel="prefetch" href="/assets/js/170.c29ec18f.js"><link rel="prefetch" href="/assets/js/171.38820827.js"><link rel="prefetch" href="/assets/js/172.bbc8ffc6.js"><link rel="prefetch" href="/assets/js/173.470e21e7.js"><link rel="prefetch" href="/assets/js/174.3c2df318.js"><link rel="prefetch" href="/assets/js/175.d2690cdb.js"><link rel="prefetch" href="/assets/js/176.9ca64696.js"><link rel="prefetch" href="/assets/js/177.76f3271d.js"><link rel="prefetch" href="/assets/js/178.d7d9def2.js"><link rel="prefetch" href="/assets/js/179.b5644743.js"><link rel="prefetch" href="/assets/js/18.31fe7ecd.js"><link rel="prefetch" href="/assets/js/180.7592d5ef.js"><link rel="prefetch" href="/assets/js/181.5cb77d35.js"><link rel="prefetch" href="/assets/js/182.6fa5633c.js"><link rel="prefetch" href="/assets/js/183.b3a53d1b.js"><link rel="prefetch" href="/assets/js/184.3815c537.js"><link rel="prefetch" href="/assets/js/185.bcf4ab71.js"><link rel="prefetch" href="/assets/js/186.1cc02f6d.js"><link rel="prefetch" href="/assets/js/187.8b425fb7.js"><link rel="prefetch" href="/assets/js/188.44ccbd02.js"><link rel="prefetch" href="/assets/js/189.353b35e3.js"><link rel="prefetch" href="/assets/js/19.520992d5.js"><link rel="prefetch" href="/assets/js/190.c284595f.js"><link rel="prefetch" href="/assets/js/191.788ecc2d.js"><link rel="prefetch" href="/assets/js/192.712a164e.js"><link rel="prefetch" href="/assets/js/193.da58aba3.js"><link rel="prefetch" href="/assets/js/194.6b1b1f4d.js"><link rel="prefetch" href="/assets/js/195.c31d5c39.js"><link rel="prefetch" href="/assets/js/196.f6670c4d.js"><link rel="prefetch" href="/assets/js/197.5a1f50ab.js"><link rel="prefetch" href="/assets/js/2.ab565158.js"><link rel="prefetch" href="/assets/js/20.69e29cdc.js"><link rel="prefetch" href="/assets/js/21.2fd424ad.js"><link rel="prefetch" href="/assets/js/22.d4c0be54.js"><link rel="prefetch" href="/assets/js/23.4bb90ecc.js"><link rel="prefetch" href="/assets/js/24.c01be6b2.js"><link rel="prefetch" href="/assets/js/25.c8833687.js"><link rel="prefetch" href="/assets/js/26.8042b555.js"><link rel="prefetch" href="/assets/js/27.0d5fa4c0.js"><link rel="prefetch" href="/assets/js/28.f9735b8b.js"><link rel="prefetch" href="/assets/js/29.3af53626.js"><link rel="prefetch" href="/assets/js/30.5f1b56d1.js"><link rel="prefetch" href="/assets/js/31.544b2649.js"><link rel="prefetch" href="/assets/js/32.aa321988.js"><link rel="prefetch" href="/assets/js/33.6aba2c86.js"><link rel="prefetch" href="/assets/js/34.e1bbff24.js"><link rel="prefetch" href="/assets/js/35.233f76e0.js"><link rel="prefetch" href="/assets/js/36.cb773972.js"><link rel="prefetch" href="/assets/js/37.393d9c59.js"><link rel="prefetch" href="/assets/js/38.e2d530c5.js"><link rel="prefetch" href="/assets/js/39.acaf1cc0.js"><link rel="prefetch" href="/assets/js/40.358f731e.js"><link rel="prefetch" href="/assets/js/41.ded24b7e.js"><link rel="prefetch" href="/assets/js/42.b9f683c3.js"><link rel="prefetch" href="/assets/js/43.c8fb3e66.js"><link rel="prefetch" href="/assets/js/44.633142da.js"><link rel="prefetch" href="/assets/js/45.6095e772.js"><link rel="prefetch" href="/assets/js/46.421d8c7a.js"><link rel="prefetch" href="/assets/js/47.da50fe47.js"><link rel="prefetch" href="/assets/js/48.15ff5726.js"><link rel="prefetch" href="/assets/js/49.b662e624.js"><link rel="prefetch" href="/assets/js/5.c1b8a209.js"><link rel="prefetch" href="/assets/js/50.a8bc75df.js"><link rel="prefetch" href="/assets/js/51.51e36ae7.js"><link rel="prefetch" href="/assets/js/53.4173561d.js"><link rel="prefetch" href="/assets/js/54.7cab8416.js"><link rel="prefetch" href="/assets/js/55.3d7317d3.js"><link rel="prefetch" href="/assets/js/56.3c22255b.js"><link rel="prefetch" href="/assets/js/57.18e46e30.js"><link rel="prefetch" href="/assets/js/58.aad57f31.js"><link rel="prefetch" href="/assets/js/59.7897f6a7.js"><link rel="prefetch" href="/assets/js/6.3131f88a.js"><link rel="prefetch" href="/assets/js/60.5cd0051a.js"><link rel="prefetch" href="/assets/js/61.d9606403.js"><link rel="prefetch" href="/assets/js/62.aede9df0.js"><link rel="prefetch" href="/assets/js/63.2c30e554.js"><link rel="prefetch" href="/assets/js/64.18228ab7.js"><link rel="prefetch" href="/assets/js/65.27cb3fba.js"><link rel="prefetch" href="/assets/js/66.2fa6c2dc.js"><link rel="prefetch" href="/assets/js/67.d274a8df.js"><link rel="prefetch" href="/assets/js/68.3069cfcf.js"><link rel="prefetch" href="/assets/js/69.4c28600f.js"><link rel="prefetch" href="/assets/js/7.89e6165d.js"><link rel="prefetch" href="/assets/js/70.4175440c.js"><link rel="prefetch" href="/assets/js/71.2ee6b435.js"><link rel="prefetch" href="/assets/js/72.c75e3bb8.js"><link rel="prefetch" href="/assets/js/73.6f8b8211.js"><link rel="prefetch" href="/assets/js/74.6c7720cf.js"><link rel="prefetch" href="/assets/js/75.cccfb229.js"><link rel="prefetch" href="/assets/js/76.f022e5da.js"><link rel="prefetch" href="/assets/js/77.dab46206.js"><link rel="prefetch" href="/assets/js/78.ca574b2a.js"><link rel="prefetch" href="/assets/js/79.3d75e618.js"><link rel="prefetch" href="/assets/js/80.091749b1.js"><link rel="prefetch" href="/assets/js/81.14db0e21.js"><link rel="prefetch" href="/assets/js/82.8a2b1809.js"><link rel="prefetch" href="/assets/js/83.84a4b599.js"><link rel="prefetch" href="/assets/js/84.11d7c222.js"><link rel="prefetch" href="/assets/js/85.273d4388.js"><link rel="prefetch" href="/assets/js/86.fb40e20c.js"><link rel="prefetch" href="/assets/js/87.3316639e.js"><link rel="prefetch" href="/assets/js/88.dfc52200.js"><link rel="prefetch" href="/assets/js/89.8d615f6e.js"><link rel="prefetch" href="/assets/js/90.1d9f08bb.js"><link rel="prefetch" href="/assets/js/91.566813e7.js"><link rel="prefetch" href="/assets/js/92.d13c6f41.js"><link rel="prefetch" href="/assets/js/93.845c42a0.js"><link rel="prefetch" href="/assets/js/94.20a37b77.js"><link rel="prefetch" href="/assets/js/95.1a498005.js"><link rel="prefetch" href="/assets/js/96.39fa7f4b.js"><link rel="prefetch" href="/assets/js/97.50f7170e.js"><link rel="prefetch" href="/assets/js/98.dd2e15d6.js"><link rel="prefetch" href="/assets/js/99.ef7ea06a.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.ae6b1de9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.952d6952.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="https://cdn.jsdelivr.net/gh/cmty256/imgs-blog@main/logo/白云.38zbldnhh180.jpg" alt="ShuangChenYue" class="logo"> <span class="site-name can-hide">ShuangChenYue</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" class="nav-link">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="CPP语言" class="dropdown-title"><!----> <span class="title" style="display:;">CPP语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c5bdd8/" class="nav-link">Cpp之旅</a></li><li class="dropdown-item"><!----> <a href="/pages/279e62/" class="nav-link">Cpp专栏</a></li><li class="dropdown-item"><!----> <a href="/pages/801755/" class="nav-link">Effective_CPP</a></li><li class="dropdown-item"><!----> <a href="/pages/6b2468/" class="nav-link">muduo网络库</a></li><li class="dropdown-item"><!----> <a href="/pages/5f8c9f/" class="nav-link">Unix环境高级编程</a></li><li class="dropdown-item"><!----> <a href="/pages/3f1d21/" class="nav-link">Cpp提高编程</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机基础" class="dropdown-title"><!----> <span class="title" style="display:;">计算机基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/7b1cb2/" class="nav-link">计算机网络</a></li><li class="dropdown-item"><!----> <a href="/pages/6048a8/" class="nav-link">操作系统</a></li><li class="dropdown-item"><!----> <a href="/pages/3b34ba/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/pages/412fe7/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/pages/2dcfa1/" class="nav-link">算法</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/efa3f2/" class="nav-link">基础篇</a></li><li class="dropdown-item"><!----> <a href="/pages/ccc445/" class="nav-link">MySql</a></li><li class="dropdown-item"><!----> <a href="/pages/54616e/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="嵌入式软件开发" class="dropdown-title"><!----> <span class="title" style="display:;">嵌入式软件开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d142c2/" class="nav-link">电子嵌入式通信协议</a></li><li class="dropdown-item"><!----> <a href="/pages/4c6bf3/" class="nav-link">深入浅出SSD</a></li><li class="dropdown-item"><!----> <a href="/pages/d3f36a/" class="nav-link">文件系统</a></li><li class="dropdown-item"><!----> <a href="/pages/e0cca7/" class="nav-link">汇编语言</a></li><li class="dropdown-item"><!----> <a href="/pages/fab2d7/" class="nav-link">STM32</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发日常" class="dropdown-title"><!----> <span class="title" style="display:;">开发日常</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/e472d1/" class="nav-link">随笔（持续更新）</a></li><li class="dropdown-item"><!----> <a href="/pages/71f6ae/" class="nav-link">Git知识总结</a></li><li class="dropdown-item"><!----> <a href="/pages/db6fb8/" class="nav-link">Git备忘清单</a></li><li class="dropdown-item"><!----> <a href="/pages/e1081f/" class="nav-link">Git 创建删除远程分支</a></li><li class="dropdown-item"><!----> <a href="/pages/777b8a/" class="nav-link">nvm使用小结</a></li><li class="dropdown-item"><!----> <a href="/pages/ee770e/" class="nav-link">虚拟机固定 IP 地址</a></li><li class="dropdown-item"><!----> <a href="/pages/1ab9a6/" class="nav-link">Shell 脚本学习笔记</a></li><li class="dropdown-item"><!----> <a href="/pages/411aa4/" class="nav-link">VScode 插件 CodeGeeX 使用教程</a></li><li class="dropdown-item"><!----> <a href="/pages/0d525d/" class="nav-link">KylinV10 将项目上传至 Github教程</a></li><li class="dropdown-item"><!----> <a href="/pages/907786/" class="nav-link">KylinV10 安装 MySQL 教程（可防踩雷）</a></li><li class="dropdown-item"><!----> <a href="/pages/a2d21e/" class="nav-link">kylinV10-SP1 安装 QT</a></li><li class="dropdown-item"><!----> <a href="/pages/b561cf/" class="nav-link">高并发内存池</a></li><li class="dropdown-item"><!----> <a href="/pages/6ab6d1/" class="nav-link">USBGUARD 项目编译环境配置</a></li><li class="dropdown-item"><!----> <a href="/pages/883f02/" class="nav-link">Power_Destory 项目</a></li><li class="dropdown-item"><!----> <a href="/pages/479472/" class="nav-link">U 盘清除工具编译教程</a></li><li class="dropdown-item"><!----> <a href="/pages/9c4241/" class="nav-link">个人博客代码推送教程</a></li><li class="dropdown-item"><!----> <a href="/pages/3ad765/" class="nav-link">SVN Trunk Branches的Merge操作</a></li><li class="dropdown-item"><!----> <a href="/pages/0c0ca8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端学习" class="dropdown-title"><!----> <span class="title" style="display:;">前端学习</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/99897f/" class="nav-link">HTML与CSS</a></li><li class="dropdown-item"><!----> <a href="/pages/51542d/" class="nav-link">JS学习</a></li><li class="dropdown-item"><!----> <a href="/pages/803f9d/" class="nav-link">Vue3入门</a></li><li class="dropdown-item"><!----> <a href="/pages/ca4cfb/" class="nav-link">Vue3进阶</a></li><li class="dropdown-item"><!----> <a href="/pages/50e8d3/" class="nav-link">黑马Vue3</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="MFC" class="dropdown-title"><!----> <span class="title" style="display:;">MFC</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/a4b108/" class="nav-link">MFC编程随记</a></li><li class="dropdown-item"><!----> <a href="/pages/41acbd/" class="nav-link">MFC实现ini配置文件的读取</a></li><li class="dropdown-item"><!----> <a href="/pages/951a7a/" class="nav-link">MFC实现点击列表头排序</a></li><li class="dropdown-item"><!----> <a href="/pages/a8598f/" class="nav-link">贴图法美化Button按钮</a></li><li class="dropdown-item"><!----> <a href="/pages/054516/" class="nav-link">MFC使用细节</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="闪存" class="dropdown-title"><!----> <span class="title" style="display:;">闪存</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/b925b8/" class="nav-link">如何高效阅读嵌入式项目代码</a></li><li class="dropdown-item"><!----> <a href="/pages/28ec23/" class="nav-link">NAND Flash</a></li><li class="dropdown-item"><!----> <a href="/pages/62bf40/" class="nav-link">ARM 处理器</a></li><li class="dropdown-item"><!----> <a href="/pages/1a9374/" class="nav-link">嵌入式基础知识-存储器</a></li><li class="dropdown-item"><!----> <a href="/pages/aac5e3/" class="nav-link">闪存存储和制造技术概述</a></li><li class="dropdown-item"><!----> <a href="/pages/8f6056/" class="nav-link">芯片IO驱动力</a></li><li class="dropdown-item"><!----> <a href="/pages/d146b8/" class="nav-link">主流先进封装技术介绍</a></li><li class="dropdown-item"><!----> <a href="/pages/16f0ba/" class="nav-link">NAND Flash基础</a></li><li class="dropdown-item"><!----> <a href="/pages/90d8d0/" class="nav-link">基于PA算法的FTL引导</a></li><li class="dropdown-item"><!----> <a href="/pages/eb672b/" class="nav-link">SD逻辑擦除和物理擦除</a></li><li class="dropdown-item"><!----> <a href="/pages/747121/" class="nav-link">NAND Flash的SDR、ONFI、DDR接口</a></li><li class="dropdown-item"><!----> <a href="/pages/1eb351/" class="nav-link">【详解】Nand Flash必看知识</a></li><li class="dropdown-item"><!----> <a href="/pages/d2512a/" class="nav-link">【两万字详解】Nand Flash必看知识</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面经" class="dropdown-title"><!----> <span class="title" style="display:;">面经</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/d69946/" class="nav-link">虎牙C++技术面经</a></li><li class="dropdown-item"><!----> <a href="/pages/29251d/" class="nav-link">金山一面复习</a></li><li class="dropdown-item"><!----> <a href="/pages/c7c01f/" class="nav-link">完美世界秋招 C++ 游戏开发面经(Cpp部分)</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/fa256e/" class="nav-link">博客搭建</a></li><li class="dropdown-item"><!----> <a href="/pages/335531/" class="nav-link">网站收藏箱</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Cpp之旅</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Cpp专栏</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/279e62/" class="sidebar-link">第2章 变量和基本类型</a></li><li><a href="/pages/518856/" class="sidebar-link">Cpp11新标准</a></li><li><a href="/pages/2f7f31/" class="sidebar-link">探索C++14新特性：更强大、更高效的编程</a></li><li><a href="/pages/c6fc35/" class="sidebar-link">C++17 新特性</a></li><li><a href="/pages/7dd194/" aria-current="page" class="active sidebar-link">协程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_1-1-进程" class="sidebar-link">1.1 进程</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_1-2-线程" class="sidebar-link">1.2 线程</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_1-3-进程和线程的区别和联系" class="sidebar-link">1.3 进程和线程的区别和联系</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_1-3-1-区别" class="sidebar-link">1.3.1 区别</a></li><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_1-3-2-联系" class="sidebar-link">1.3.2 联系：</a></li><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_1-3-3-举例说明进程和线程区别" class="sidebar-link">1.3.3 举例说明进程和线程区别</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_1-4-进程-线程之间的亲缘性" class="sidebar-link">1.4 进程/线程之间的亲缘性</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_3-1-有栈协程和无栈协程" class="sidebar-link">3.1 有栈协程和无栈协程</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_3-2-c-20-为什么选择无栈协程" class="sidebar-link">3.2 C++20 为什么选择无栈协程？</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_3-3-无栈协程是普通函数的泛化" class="sidebar-link">3.3 无栈协程是普通函数的泛化</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_3-4-c-20-协程的三个关键字" class="sidebar-link">3.4 C++20 协程的三个关键字</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#协程相关的对象" class="sidebar-link">协程相关的对象</a></li><li class="sidebar-sub-header level4"><a href="/pages/7dd194/#协程帧-coroutine-frame" class="sidebar-link">协程帧(coroutine frame)</a></li><li class="sidebar-sub-header level4"><a href="/pages/7dd194/#promise-type" class="sidebar-link">promise_type</a></li><li class="sidebar-sub-header level4"><a href="/pages/7dd194/#coroutine-return-object" class="sidebar-link">coroutine return object</a></li><li class="sidebar-sub-header level4"><a href="/pages/7dd194/#std-coroutine-handle" class="sidebar-link">std::coroutine_handle</a></li><li class="sidebar-sub-header level4"><a href="/pages/7dd194/#co-await、awaiter、awaitable" class="sidebar-link">co_await、awaiter、awaitable</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_4-1-ucontext-结构体" class="sidebar-link">4.1 ucontext 结构体</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_4-2-ucontext-相关函数" class="sidebar-link">4.2 ucontext 相关函数</a></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_4-3-协程框架设计与实现" class="sidebar-link">4.3 协程框架设计与实现</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_4-3-1-协程的状态" class="sidebar-link">4.3.1 协程的状态</a></li><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_4-3-2-协程类-routine" class="sidebar-link">4.3.2 协程类 Routine</a></li><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_4-3-4-栈增长方向" class="sidebar-link">4.3.4 栈增长方向</a></li><li class="sidebar-sub-header level3"><a href="/pages/7dd194/#_4-3-5-调度器-schedule" class="sidebar-link">4.3.5 调度器 Schedule</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7dd194/#_4-4-协程框架应用" class="sidebar-link">4.4 协程框架应用</a></li></ul></li><li><a href="/pages/e769c6/" class="sidebar-link">C++ 备忘清单</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Effetcive_CPP</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>muduo网络库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Unix环境高级编程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Cpp提高编程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><span data-v-06225672>CPP语言</span></li><li data-v-06225672><span data-v-06225672>Cpp专栏</span></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="javascript:;" data-v-06225672>霜晨月</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-01-17</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">协程<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="协程"><a href="#协程" class="header-anchor">#</a> 协程</h1> <blockquote><p>要想了解协程，最好先搞清楚进程，线程，这样才能将三者区分开来！</p></blockquote> <h1 id="进程-vs-线程-vs-协程"><a href="#进程-vs-线程-vs-协程" class="header-anchor">#</a> 进程 vs 线程 vs 协程</h1> <table><thead><tr><th></th> <th>进程</th> <th>线程</th> <th>协程</th></tr></thead> <tbody><tr><td>切换者</td> <td>操作系统</td> <td>操作系统</td> <td>用户（编程者）</td></tr> <tr><td>切换时机</td> <td>根据操作系统自己的切换策略，用户不感知</td> <td>根据操作系统自己的切换策略，用户不感知</td> <td>用户（编程者）自己决定</td></tr> <tr><td>切换内容</td> <td>页全局目录、内核栈、硬件上下文</td> <td>内核栈、硬件上下文</td> <td>硬件上下文</td></tr> <tr><td>切换内容的保存</td> <td>保存于内核栈中</td> <td>保存于内核栈中</td> <td>保存于用户栈中</td></tr> <tr><td>切换过程</td> <td>用户态-内核态-用户态</td> <td>用户态-内核态-用户态</td> <td>用户态（没有陷入内核态）</td></tr></tbody></table> <p><strong>协程优点：</strong></p> <ol><li>I/O 阻塞时，利用协程来处理（切换效率比较高）</li> <li>不需要锁机制</li></ol> <p><strong>协程缺点：</strong></p> <ol><li>不能利用多核 CPU</li></ol> <h1 id="_1-进程和线程"><a href="#_1-进程和线程" class="header-anchor">#</a> 1. 进程和线程</h1> <h2 id="_1-1-进程"><a href="#_1-1-进程" class="header-anchor">#</a> 1.1 进程</h2> <p>进程，直观点说，保存在硬盘上的程序运行以后，会在内存空间里形成一个独立的内存体，这个内存体 <strong>有自己独立的地址空间，有自己的堆</strong> ，上级挂靠单位是操作系统。 <strong>操作系统会以进程为单位，分配系统资源（CPU时间片、内存等资源），进程是资源分配的最小单位</strong> 。</p> <p><img alt="1706250365964" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706250365964.webp" loading="lazy" class="lazy"></p> <p><img alt="1706250434187" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706250434187.webp" loading="lazy" class="lazy"></p> <p>【进程间通信（IPC）】：</p> <ul><li>管道(Pipe)、命名管道(FIFO)、消息队列(Message Queue) 、信号量(Semaphore) 、共享内存（Shared Memory）；套接字（Socket）。</li></ul> <h2 id="_1-2-线程"><a href="#_1-2-线程" class="header-anchor">#</a> 1.2 线程</h2> <p><strong>线程，有时被称为轻量级进程(Lightweight Process，LWP），是操作系统调度（CPU调度）执行的最小单位</strong>。</p> <p><img alt="1706250470527" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706250470527.webp" loading="lazy" class="lazy"></p> <h2 id="_1-3-进程和线程的区别和联系"><a href="#_1-3-进程和线程的区别和联系" class="header-anchor">#</a> 1.3 进程和线程的区别和联系</h2> <h3 id="_1-3-1-区别"><a href="#_1-3-1-区别" class="header-anchor">#</a> 1.3.1 区别</h3> <ul><li><strong>调度：</strong> 线程作为调度和分配的基本单位，进程作为拥有资源的基本单位；</li> <li><strong>并发性：</strong> 不仅进程之间可以并发执行，同一个进程的多个线程之间也可并发执行；</li> <li><strong>拥有资源：</strong> 进程是拥有资源的一个独立单位，线程不拥有系统资源，但可以访问隶属于进程的资源。进程所维护的是程序所包含的资源（静态资源）， 如：地址空间，打开的文件句柄集，文件系统状态，信号处理 handler 等；线程所维护的运行相关的资源（动态资源），如：运行栈，调度相关的控制信息，待处理的信号集等；</li> <li><strong>系统开销：</strong> 在创建或撤消进程时，由于系统都要为之分配和回收资源，导致系统的开销明显大于创建或撤消线程时的开销。但是进程有独立的地址空间，一个进程崩溃后，在保护模式下不会对其它进程产生影响，而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量，但线程之间没有单独的地址空间，一个进程死掉就等于所有的线程死掉，所以多进程的程序要比多线程的程序健壮，但在进程切换时，耗费资源较大，效率要差一些。</li></ul> <h3 id="_1-3-2-联系"><a href="#_1-3-2-联系" class="header-anchor">#</a> 1.3.2 联系：</h3> <ul><li><strong>一个线程只能属于一个进程，而一个进程可以有多个线程，但至少有一个线程</strong>；</li> <li>资源分配给进程，同一进程的所有线程共享该进程的所有资源；</li> <li>处理机分给线程，即<strong>真正在处理机上运行的是线程</strong>；</li> <li>线程在执行过程中，需要协作同步。不同进程的线程间要利用消息通信的办法实现同步。</li></ul> <h3 id="_1-3-3-举例说明进程和线程区别"><a href="#_1-3-3-举例说明进程和线程区别" class="header-anchor">#</a> 1.3.3 举例说明进程和线程区别</h3> <p>假如我们把整条道路看成是一个 “进程” 的话，那么由白色虚线分隔开来的各个车道就是进程中的各个 “线程” 了。</p> <ul><li><p>这些 <strong>线程(车道)</strong> 共享了 <strong>进程(道路)</strong> 的公共资源(土地资源)。</p></li> <li><p>这些 <strong>线程(车道)</strong> 必须依赖于 <strong>进程(道路)</strong> ，也就是说，线程不能脱离于进程而存在(就像离开了道路，车道也就没有意义了)。</p></li> <li><p>这些 <strong>线程(车道)</strong> 之间可以 <strong>并发执行(各个车道你走你的，我走我的)</strong> ，也可以 <strong>互相同步(某些车道在交通灯亮时禁止继续前行或转弯，必须等待其它车道的车辆通行完毕)</strong> 。</p></li> <li><p>这些 <strong>线程(车道)</strong> 之间依靠 <strong>代码逻辑(交通灯)</strong> 来控制运行，一旦 <strong>代码逻辑控制有误(死锁，多个线程同时竞争唯一资源)</strong> ，那么线程将陷入混乱，无序之中。</p></li></ul> <h2 id="_1-4-进程-线程之间的亲缘性"><a href="#_1-4-进程-线程之间的亲缘性" class="header-anchor">#</a> 1.4 进程/线程之间的亲缘性</h2> <p><strong>亲缘性的意思是进程/线程只在某个 CPU 上运行（多核系统）</strong>，比如：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code>BOOL WINAPI <span class="token function">SetProcessAffinityMask</span><span class="token punctuation">(</span>
  _In_ HANDLE    hProcess<span class="token punctuation">,</span>
  _In_ DWORD_PTR dwProcessAffinityMask
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/*
dwProcessAffinityMask 如果是 0 , 代表当前进程只在 cpu0 上工作;
如果是 0x03 , 转为2进制是 00000011 . 代表只在 cpu0 或 cpu1 上工作;
*/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>使用CPU亲缘性的好处：</strong></p> <p><strong>设置CPU亲缘性是为了防止进程/线程在CPU的核上频繁切换</strong> ，从而 <strong>避免因切换带来的CPU的L1/L2 cache失效</strong> ，cache 失效会降低程序的性能。</p> <h1 id="_2-协程"><a href="#_2-协程" class="header-anchor">#</a> 2. 协程</h1> <blockquote><p>概要速览：</p> <p>协程（coroutine）是一种在程序设计中用于实现多任务并发执行的技术。它允许在单个线程内进行多个任务之间的切换，这种切换是协作式的，即任务主动让出控制权，而不是抢占式的。协程的概念最早可以追溯到1958年，由马尔文·康威（Marvin Conway）提出，并在1963年首次公开发表。</p> <p>在程序中，协程通常通过函数或方法的暂停和恢复来实现。一个协程函数可以在执行到某个点时暂停，将控制权让给其他的协程函数，并在适当的时候恢复执行。这种机制使得一个线程可以在等待某些操作（如I/O）完成时切换到其他任务，从而提高资源的利用率。</p> <p>协程的实现通常依赖于事件循环（event loop）或类似的机制，用于调度和管理协程的执行。在许多编程语言中，协程已经成为并发编程的标准库部分，如 Python 中的 asyncio 模块、Go 语言中的协程以及 Kotlin 中的协程支持。</p> <p>协程的使用场景非常广泛，包括但不限于：</p> <ul><li>异步 I/O 操作：在网络请求或文件操作中，协程可以在等待 I/O 完成时执行其他任务。</li> <li>并发任务调度：在图形用户界面（GUI）应用程序中，协程可以实现平滑的用户界面响应，同时处理后台任务。</li> <li>协程异常处理：协程支持异常的传播和捕获，允许在协程结构中传播异常信息。</li></ul> <p>协程的一个重要特性是它们可以携带状态，这意味着当协程被挂起时，它当前的状态（包括局部变量和程序计数器等）会被保存，并在恢复执行时恢复到挂起前的状态。</p> <p>在不同的编程语言和平台中，协程的实现和调度机制可能有所不同，但其核心概念和目的是相似的：通过协作式的多任务并发执行，提高程序的性能和响应能力。</p></blockquote> <p><img alt="1706250775284" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706250775284.webp" loading="lazy" class="lazy"></p> <p>协程，是一种比线程更加轻量级的存在，协程不是被操作系统内核所管理，而完全是由程序所控制（也就是在用户态执行）。这样带来的好处就是性能得到了很大的提升，不会像线程切换那样消耗资源。</p> <p>子程序，或者称为函数，在所有语言中都是层级调用，比如 A 调用 B，B 在执行过程中又调用了 C，C 执行完毕返回，B 执行完毕返回，最后是 A 执行完毕。所以子程序调用是通过栈实现的，<strong>一个线程就是执行一个子程序</strong>。子程序调用总是一个入口，一次返回，调用顺序是明确的。而协程的调用和子程序不同。</p> <p>==<strong>协程在子程序内部是可中断的，然后转而执行别的子程序，在适当的时候再返回来接着执行</strong>。==</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code>def <span class="token function">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span>
    print <span class="token char">'1'</span>
    print <span class="token char">'2'</span>
    print <span class="token char">'3'</span>

def <span class="token function">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span>
    print <span class="token char">'x'</span>
    print <span class="token char">'y'</span>
    print <span class="token char">'z'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>假设由协程执行，在执行A的过程中，可以随时中断，去执行B，B也可能在执行过程中中断再去执行A，结果可能是：<code>1 2 x y 3 z</code>。</p> <p>协程的特点在于是一个线程执行，那和多线程比，协程有何优势？</p> <ul><li><p>极高的执行效率：因为子程序切换不是线程切换，而是由程序自身控制，因此，没有线程切换的开销，和多线程比，线程数量越多，协程的性能优势就越明显；</p></li> <li><p>不需要多线程的锁机制：因为只有一个线程，也不存在同时写变量冲突，在协程中控制共享资源不加锁，只需要判断状态就好了，所以执行效率比多线程高很多。</p></li></ul> <h1 id="_3-c-20-协程原理和应用"><a href="#_3-c-20-协程原理和应用" class="header-anchor">#</a> 3. C++20 协程原理和应用</h1> <h2 id="_3-1-有栈协程和无栈协程"><a href="#_3-1-有栈协程和无栈协程" class="header-anchor">#</a> 3.1 有栈协程和无栈协程</h2> <blockquote><p>所谓的有栈，无栈并不是说这个协程运行的时候有没有栈，而是说协程之间是否存在调用栈（callbackStack）。</p></blockquote> <p><strong>协程可分为两种：</strong></p> <ul><li>无栈协程：即可挂起/恢复的函数，无栈协程切换的成本相当于函数调用的成本。</li> <li>有栈协程：即相当于用户态线程，有栈协程切换的成本是用户态线程切换的成本。</li></ul> <p><strong>无栈协程和线程的区别：</strong></p> <ul><li>无栈协程只能被线程调用，本身并不抢占内核调度，而线程则可抢占内核调度。</li></ul> <blockquote><p>C++20 协程中采纳的是微软提出并主导（源于C#）的无栈协程。</p> <p>C++世界演化的主旋律：异步化和并行化，而 C++20 协程能够以同步原语写异步代码的特性，使其成为编写异步代码的好工具。</p></blockquote> <p>具体可以看这篇文章：https://blog.csdn.net/weixin_43705457/article/details/106924435</p> <h2 id="_3-2-c-20-为什么选择无栈协程"><a href="#_3-2-c-20-为什么选择无栈协程" class="header-anchor">#</a> 3.2 C++20 为什么选择无栈协程？</h2> <p>有栈（stackful）协程通常的实现手段是在堆上提前分配一块较大的内存空间（比如 64K），也就是协程所谓的“栈”，参数、return address 等都可以存放在这个“栈”空间上。如果需要协程切换，那么通过 swapcontext 一类的形式来让系统认为这个堆上空间就是普通的栈，这就实现了上下文的切换。</p> <blockquote><p>有栈协程最大的优势就是侵入性小，使用起来非常简便，已有的业务代码几乎不需要做什么修改，但是 C++20 最终还是选择了使用无栈协程，主要出于下面这几个方面的考虑。</p></blockquote> <ul><li><p><strong>栈空间的限制：</strong></p> <p>有栈协程的“栈”空间普遍是比较小的，在使用中有栈溢出的风险；而如果让“栈”空间变得很大，对内存空间又是很大的浪费。无栈协程则没有这些限制，既没有溢出的风险，也无需担心内存利用率的问题。</p></li> <li><p><strong>性能：</strong></p> <p>有栈协程在切换时确实比系统线程要轻量，但是和无栈协程相比仍然是偏重的，这一点虽然在我们目前的实际使用中影响没有那么大（异步系统的使用通常伴随了 IO，相比于切换开销多了几个数量级），但也决定了无栈协程可以用在一些更有意思的场景上。</p></li></ul> <h2 id="_3-3-无栈协程是普通函数的泛化"><a href="#_3-3-无栈协程是普通函数的泛化" class="header-anchor">#</a> 3.3 无栈协程是普通函数的泛化</h2> <blockquote><p>无栈协程是一个可以暂停和恢复的函数，是函数调用的泛化。</p></blockquote> <p>一个函数的函数体(function body)是顺序执行的，执行完之后将结果返回给调用者，我们没办法挂起它并稍后恢复它，只能等待它结束。而无栈协程则允许我们把函数挂起，然后在任意需要的时刻去恢复并执行函数体，相比普通函数，协程的函数体可以挂起并在任意时刻恢复执行。</p> <p><img alt="1706250841310" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706250841310.webp" loading="lazy" class="lazy"></p> <p>所以，从这个角度来说，无栈协程是普通函数的泛化。</p> <h2 id="_3-4-c-20-协程的三个关键字"><a href="#_3-4-c-20-协程的三个关键字" class="header-anchor">#</a> 3.4 C++20 协程的三个关键字</h2> <p>C++20 提供了三个新关键字(co_await、co_yield 和 co_return)，如果一个函数中存在这三个关键字之一，那么它就是一个协程。</p> <h3 id="协程相关的对象"><a href="#协程相关的对象" class="header-anchor">#</a> 协程相关的对象</h3> <h4 id="协程帧-coroutine-frame"><a href="#协程帧-coroutine-frame" class="header-anchor">#</a> 协程帧(coroutine frame)</h4> <p>当 caller 调用一个协程的时候会先创建一个协程帧，协程帧会构建 promise 对象，再通过 promise 对象产生 return object。</p> <p><strong>协程帧中主要有这些内容：</strong></p> <ul><li><p>协程参数</p></li> <li><p>局部变量</p></li> <li><p>promise 对象</p></li></ul> <h4 id="promise-type"><a href="#promise-type" class="header-anchor">#</a> promise_type</h4> <p>promise_type 是 promise 对象的类型。promise_type 用于定义一类协程的行为，包括协程创建方式、协程初始化完成和结束时的行为、发生异常时的行为、如何生成 awaiter 的行为以及 co_return 的行为等等。promise 对象可以用于记录/存储一个协程实例的状态。每个协程桢与每个 promise 对象以及每个协程实例是一一对应的。</p> <h4 id="coroutine-return-object"><a href="#coroutine-return-object" class="header-anchor">#</a> coroutine return object</h4> <p>它是 promise.get_return_object() 方法创建的，一种常见的实现手法会将 coroutine_handle 存储到 coroutine object 内，使得该 return object 获得访问协程的能力。</p> <h4 id="std-coroutine-handle"><a href="#std-coroutine-handle" class="header-anchor">#</a> std::coroutine_handle</h4> <p>协程帧的句柄，主要用于访问底层的协程帧、恢复协程和释放协程帧。
程序员可通过调用 std::coroutine_handle::resume() 唤醒协程。</p> <h4 id="co-await、awaiter、awaitable"><a href="#co-await、awaiter、awaitable" class="header-anchor">#</a> co_await、awaiter、awaitable</h4> <ul><li><p>co_await：一元操作符；</p></li> <li><p>awaitable：支持 co_await 操作符的类型；</p></li> <li><p>awaiter：定义了 await_ready、await_suspend 和 await_resume 方法的类型。</p></li></ul> <p>co_await expr 通常用于表示等待一个任务(可能是 lazy 的，也可能不是)完成。co_await expr 时，expr 的类型需要是一个 awaitable，而该 co_await 表达式的具体语义取决于根据该 awaitable 生成的 awaiter。</p> <p>看起来和协程相关的对象还不少，这正是协程复杂又灵活的地方，可以借助这些对象来实现对协程的完全控制，实现任何想法。但是，需要先要了解这些对象是如何协作的，把这个搞清楚了，协程的原理就掌握了，写协程应用也会游刃有余了。</p> <h1 id="_4-如何实现协程"><a href="#_4-如何实现协程" class="header-anchor">#</a> 4. 如何实现协程</h1> <p>https://hkrb7870j3.feishu.cn/docx/RUMldRX8koIgxIxvAmccy4PYnCc</p> <p><strong>利用操作系统提供的接口来实现协程：</strong></p> <ol><li>Linux 的 ucontext（下文示例都是用这个）</li> <li>Windows 的 Fiber</li></ol> <h2 id="_4-1-ucontext-结构体"><a href="#_4-1-ucontext-结构体" class="header-anchor">#</a> 4.1 ucontext 结构体</h2> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">ucontext</span> <span class="token punctuation">{</span>
    <span class="token keyword">struct</span> <span class="token class-name">ucontext</span>     <span class="token operator">*</span>uc_link<span class="token punctuation">;</span>
    sigset_t             uc_sigmask<span class="token punctuation">;</span>
    stack_t              uc_stack<span class="token punctuation">;</span>
    mcontext_t           uc_mcontext<span class="token punctuation">;</span>
    <span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">int</span>    uc_flags<span class="token punctuation">;</span>
<span class="token punctuation">}</span> ucontext_t<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>uc_link：当前上下文(如使用 makecontext 创建的上下文）运行终止时系统会恢复 uc_link 指向的上下文</li> <li>uc_sigmask：上下文中的阻塞信号集合</li> <li>uc_stack：上下文中使用的栈</li> <li>uc_mcontext：保存的上下文的寄存器信息</li></ul> <h2 id="_4-2-ucontext-相关函数"><a href="#_4-2-ucontext-相关函数" class="header-anchor">#</a> 4.2 ucontext 相关函数</h2> <ul><li>getcontext：保存上下文，将当前运行到的寄存器的信息保存到 ucontext_t 结构体中。</li> <li>setcontext：恢复上下文，将 ucontext_t 结构体变量中的上下文信息重新恢复到 CPU 中并执行。</li> <li>makecontext：修改上下文，给 ucontext_t 上下文指定一个程序入口函数，让程序从该入口函数开始执行。</li> <li>swapcontext：切换上下文，保存当前上下文，并将下一个要执行的上下文恢复到 CPU 中。</li></ul> <p>具体 ucontext 函数族可以看这篇文章：</p> <p>https://blog.csdn.net/qq_44443986/article/details/117739157</p> <p>示例一：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;ucontext.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    
    ucontext_t ctx<span class="token punctuation">;</span>             <span class="token comment">//定义上下文结构体变量</span>
    <span class="token function">getcontext</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//获取当前上下文</span>
    
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;i = %d\n&quot;</span><span class="token punctuation">,</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">setcontext</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">//恢复上下文</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>示例二：</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;ucontext.h&gt;</span></span>

<span class="token keyword">void</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;this is child context\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">char</span> stack<span class="token punctuation">[</span><span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">64</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    ucontext_t child<span class="token punctuation">,</span> main<span class="token punctuation">;</span>

    <span class="token comment">// 获取当前上下文</span>
    <span class="token function">getcontext</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>child<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 分配栈空间, uc_stack.ss_sp 指向栈顶</span>
    child<span class="token punctuation">.</span>uc_stack<span class="token punctuation">.</span>ss_sp <span class="token operator">=</span> stack<span class="token punctuation">;</span>
    child<span class="token punctuation">.</span>uc_stack<span class="token punctuation">.</span>ss_size <span class="token operator">=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>stack<span class="token punctuation">)</span><span class="token punctuation">;</span>
    child<span class="token punctuation">.</span>uc_stack<span class="token punctuation">.</span>ss_flags <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token comment">// 指定后续的上下文</span>
    child<span class="token punctuation">.</span>uc_link <span class="token operator">=</span> <span class="token operator">&amp;</span>main<span class="token punctuation">;</span>

    <span class="token comment">//切换到child上下文，保存当前上下文到main</span>
    <span class="token function">makecontext</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>child<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">)</span>foo<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 如果设置了后继上下文，foo函数执行结束会返回此处 如果设置为NULL，就不会执行这一步</span>
    <span class="token function">swapcontext</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>main<span class="token punctuation">,</span> <span class="token operator">&amp;</span>child<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;this is main context\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h2 id="_4-3-协程框架设计与实现"><a href="#_4-3-协程框架设计与实现" class="header-anchor">#</a> 4.3 协程框架设计与实现</h2> <h3 id="_4-3-1-协程的状态"><a href="#_4-3-1-协程的状态" class="header-anchor">#</a> 4.3.1 协程的状态</h3> <ol><li><strong>RT_READY</strong>：就绪态</li> <li><strong>RT_RUNNING</strong>：运行态</li> <li><strong>RT_SUSPEND</strong>：挂起态</li></ol> <h3 id="_4-3-2-协程类-routine"><a href="#_4-3-2-协程类-routine" class="header-anchor">#</a> 4.3.2 协程类 Routine</h3> <p><strong>协程的方法：</strong></p> <ol><li><strong>run</strong>：纯虚函数，需要子类来实现协程的具体业务逻辑。</li> <li><strong>resume</strong>：唤醒协程来执行。</li> <li><strong>yield</strong>：当前协程  &quot;放弃&quot; 执行，让调度器调度另一个协程继续执行。</li></ol> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Routine</span> <span class="token punctuation">{</span>
    <span class="token keyword">friend</span> <span class="token keyword">class</span> <span class="token class-name">Schedule</span><span class="token punctuation">;</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">enum</span> <span class="token class-name">Status</span> <span class="token punctuation">{</span>
        RT_READY <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
        RT_RUNNING<span class="token punctuation">,</span>
        RT_SUSPEND
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token function">Routine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">virtual</span> <span class="token operator">~</span><span class="token function">Routine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">void</span> <span class="token function">yield</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> <span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">protected</span><span class="token operator">:</span>
    <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">func</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span> ptr<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">private</span><span class="token operator">:</span>
    ucontext_t m_ctx<span class="token punctuation">;</span>
    <span class="token keyword">int</span> m_status<span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> m_stack<span class="token punctuation">;</span>
    <span class="token keyword">int</span> m_stack_size<span class="token punctuation">;</span>
    Schedule <span class="token operator">*</span> m_s<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_4-3-4-栈增长方向"><a href="#_4-3-4-栈增长方向" class="header-anchor">#</a> 4.3.4 栈增长方向</h3> <p><img alt="1706252701674" data-src="https://jsd.cdn.zzko.cn/gh/xiaose-code/Pictures@main/img/1706252701674.webp" loading="lazy" class="lazy"></p> <h3 id="_4-3-5-调度器-schedule"><a href="#_4-3-5-调度器-schedule" class="header-anchor">#</a> 4.3.5 调度器 Schedule</h3> <p><strong>调度器的方法：</strong></p> <ol><li><strong>create</strong>：初始化协程调度器</li> <li><strong>append</strong>：添加一个新协程到队列</li> <li><strong>resume</strong>：唤醒队列里某个协程来执行</li></ol> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Schedule</span> <span class="token punctuation">{</span>
    <span class="token keyword">friend</span> <span class="token keyword">class</span> <span class="token class-name">Routine</span><span class="token punctuation">;</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Schedule</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">~</span><span class="token function">Schedule</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">create</span><span class="token punctuation">(</span><span class="token keyword">int</span> stack_size <span class="token operator">=</span> <span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">void</span> <span class="token function">append</span><span class="token punctuation">(</span>Routine <span class="token operator">*</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">bool</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">private</span><span class="token operator">:</span>
    ucontext_t m_main<span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> m_stack<span class="token punctuation">;</span>
    <span class="token keyword">int</span> m_stack_size<span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>list<span class="token operator">&lt;</span>Routine <span class="token operator">*</span><span class="token operator">&gt;</span> m_queue<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h2 id="_4-4-协程框架应用"><a href="#_4-4-协程框架应用" class="header-anchor">#</a> 4.4 协程框架应用</h2> <p>定义一个 A 线程：a_routine.h</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">once</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;routine/routine.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> yazi<span class="token double-colon punctuation">::</span>routine<span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">ARoutine</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Routine</span></span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">ARoutine</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">*</span> num<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Routine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">m_num</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token operator">~</span><span class="token function">ARoutine</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>m_num <span class="token operator">!=</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">delete</span> m_num<span class="token punctuation">;</span>
            m_num <span class="token operator">=</span> <span class="token keyword">nullptr</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token operator">*</span>m_num<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;a run: num=&quot;</span> <span class="token operator">&lt;&lt;</span> i <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
            <span class="token function">yield</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token keyword">int</span> <span class="token operator">*</span> m_num<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>main.cpp</p> <div class="language-CPP line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;routine/schedule.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;test/a_routine.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;test/b_routine.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> yazi<span class="token double-colon punctuation">::</span>routine<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    Schedule s<span class="token punctuation">;</span>
    s<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    Routine <span class="token operator">*</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">ARoutine</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    s<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>

    Routine <span class="token operator">*</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">BRoutine</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    s<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>

    s<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>视频讲解：【c++20协程太难用了，我还是自己设计一个吧】 https://www.bilibili.com/video/BV1SR4y1Y7tb/?share_source=copy_web</p> <h1 id="文章参考"><a href="#文章参考" class="header-anchor">#</a> 文章参考：</h1> <p>https://blog.csdn.net/sanmi8276/article/details/111375619</p> <p>https://blog.csdn.net/weixin_43705457/article/details/106924435</p> <p>https://hkrb7870j3.feishu.cn/docx/RUMldRX8koIgxIxvAmccy4PYnCc</p> <p>https://blog.csdn.net/qq_44443986/article/details/117739157</p> <p>https://blog.csdn.net/tennysonsky/article/details/46046317</p> <p>https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868328689835ecd883d910145dfa8227b539725e5ed000</p> <p>https://www.cnblogs.com/work115/p/5620272.html</p> <p>https://blog.csdn.net/liu251890347/article/details/38509943</p> <p>https://www.cnblogs.com/fah936861121/articles/8043187.html</p> <p>http://blog.chinaunix.net/uid-25601623-id-5095687.html</p> <p>http://t.csdnimg.cn/RolRj</p> <p>http://t.csdnimg.cn/s6wn0</p></div></div> <!----> <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/6/3 14:54:44</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/c6fc35/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">C++17 新特性</div></a> <a href="/pages/e769c6/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">C++ 备忘清单</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/c6fc35/" class="prev">C++17 新特性</a></span> <span class="next"><a href="/pages/e769c6/">C++ 备忘清单</a>→
      </span></p></div></div></div> <!----></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2025
    <span>霜晨月</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.67adcfd9.js" defer></script><script src="/assets/js/4.9aaa1650.js" defer></script><script src="/assets/js/1.5474518c.js" defer></script><script src="/assets/js/3.593d14fc.js" defer></script><script src="/assets/js/52.54cc6e51.js" defer></script>
  </body>
</html>
